package com.tssf.car_manage.controller;

import com.github.pagehelper.PageInfo;
import com.tssf.car_manage.entity.Customer;
import com.tssf.car_manage.entity.Vehicle;
import com.tssf.car_manage.service.CustomerService;
import com.tssf.car_manage.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/vehicle")
@Controller
public class VehicleController {
    @Autowired
    VehicleService vehicleService;
    @Autowired
    CustomerService customerService;

    @GetMapping("/lists")
    public String lists(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "key", required = false) String key,
            Model model) {

        int pageSize = 10; // 每页10条记录
        PageInfo<Vehicle> pageInfo;

        if (key != null && !key.isEmpty()) {
            pageInfo = vehicleService.searchWithPage(key, page, pageSize);
            model.addAttribute("key", key);
        } else {
            pageInfo = vehicleService.listWithPage(page, pageSize);
        }

        model.addAttribute("vehicleList", pageInfo.getList());
        model.addAttribute("currentPage", pageInfo.getPageNum());
        model.addAttribute("totalPages", pageInfo.getPages());

        return "vehicle/vehicle-list";
    }

    @GetMapping("/search")
    public String Sealist(
            @RequestParam("license_plate") String license_plate,
            @RequestParam(value = "page", defaultValue = "1") int page,
            Model model) {

        int pageSize = 10;
        PageInfo<Vehicle> pageInfo = vehicleService.searchWithPage(license_plate, page, pageSize);

        model.addAttribute("vehicleList", pageInfo.getList());
        model.addAttribute("currentPage", pageInfo.getPageNum());
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("key", license_plate);

        return "vehicle/vehicle-list";
    }

    @GetMapping("/delvehicle/{vehicle_id}")
    public String delvehicle(@PathVariable("vehicle_id") int vehicle_id, Model model) {
        try {
            vehicleService.delVehicle(vehicle_id);
            return "redirect:/vehicle/lists";
        } catch (Exception e) {
            model.addAttribute("errorMessage", "删除车辆失败，请检查关联数据！");
            PageInfo<Vehicle> pageInfo = vehicleService.listWithPage(1, 10);
            model.addAttribute("vehicleList", pageInfo.getList());
            model.addAttribute("currentPage", 1);
            model.addAttribute("totalPages", pageInfo.getPages());
            return "vehicle/vehicle-list";
        }
    }

    @GetMapping("/toaddvehicle")
    public String addvehicle(Model model){
        List<Customer> list = customerService.findAll();
        model.addAttribute("customerList", list);
        model.addAttribute("nextVehicleId", vehicleService.getNextVehicleId());
        return "vehicle/vehicle-add";
    }

    @PostMapping("/addvehicle")
    public String addvehicle(Vehicle vehicle){
        vehicleService.addvehicle(vehicle);
        return "redirect:/vehicle/lists";
    }

    @GetMapping("/edit/{vehicle_id}")
    public String toEditVehicle(@PathVariable("vehicle_id") int vehicle_id, Model model) {
        Vehicle vehicle = vehicleService.getById(vehicle_id);
        List<Customer> customers = customerService.findAll();

        model.addAttribute("vehicle", vehicle);
        model.addAttribute("customerList", customers);
        return "vehicle/vehicle-edit";
    }

    @PostMapping("/update")
    public String updateVehicle(Vehicle vehicle) {
        vehicleService.update(vehicle);
        return "redirect:/vehicle/lists";
    }
}